<?php 

namespace App\Repositories;
use Carbon\Carbon;

class OtherOrderRepository extends Repository 
{
    public function model() 
    {
        return 'App\Models\OtherOrder';
    }    

    public function paginateList($where = [], $page = 15)
    {
    	$where = $where->filter(function ($value) { return $value; });

    	$model = $this->model->with('designer', 'programmer', 'inputer');

        // 客户名称
        if ($where->has('word')) {

        	$word = trim($where->get('word'));
            $model = $model->where(function ($query) use ($word) {
                $query->where('name', 'like', "%{$word}%")->orWhere('domain', 'like', "%{$word}%");
            });
        }

        // 订单类型
        $where->get('type') && $model = $model->where('type', $where->get('type'));

        // 设计师
        if ($where->get('designer_id')) {
            $designerId = $where->get('designer_id');
            $model = $model->where(function($query) use ($designerId) {
                $query->where('designer_id', $designerId)->orWhere('programmer_id', $designerId); 
            });
        } 

        // 状态
        if ($where->get('status')) {
            $model = $model->where('status', $where->get('status'));
        }

        // 时间筛选
        if ($where->has('start') && $where->has('end')) {

            $dateTypeArray = [2=>'created_at', 3=>'designed_at', 4=>'produced_at', 5=>'finished_at'];

            if (isset($dateTypeArray[$where->get('date_field')])) {
                $dateField = $dateTypeArray[$where->get('date_field')];

                $start = Carbon::createFromFormat('Y-m-d', $where->get('start'))->startOfDay();
                $end   = Carbon::createFromFormat('Y-m-d', $where->get('end'))->endOfDay(); 
                 
                $model = $model->whereBetween($dateField, [$start, $end]);
            }
        }

        return $model->orderBy('status')->paginate($page);
    }

    // 统计其他订单完成量
    public function getOtherCountPeriod($range)
    {
        // 下单
        $create = $this->model->whereBetween('created_at', $range)->count();

        // 下单详细
        $createDetail = $this->model->whereBetween('created_at', $range)->select(\DB::raw("ptype, count(*) as count"))
                              ->groupBy('ptype')->lists('count', 'ptype');

        // 开发单
        $createDevelop = $this->model->whereBetween('created_at', $range)->whereIn('type', [1, 13])->count();

        // 设计确认
        $design = $this->model->whereBetween('designed_at', $range)->where('ptype', 1)->count();

        // 程序确认
        $program = $this->model->whereBetween('produced_at', $range)->where('ptype', 1)->count();

        // PSD确认
        $psd = $this->model->whereBetween('produced_at', $range)->where('ptype', 2)->count();
        
        // 总完成
        $online = $this->model->whereBetween('finished_at', $range)->count();
        // 网站完成
        $webOnline = $this->model->whereBetween('finished_at', $range)->where('ptype', '<', 3)->count();

        
        return compact('create', 'createDetail', 'createDevelop', 'design', 'program', 'psd', 'online', 'webOnline');
    }

    // 下单量
    public function getCountRange($stage, $uid, $range)
    {
        $data = array();

        switch ($stage) {
            case 'create.design':
                $data = $this->model->whereBetween('created_at', $range)->where('designer_id', $uid)->where('ptype', 1)->count();
                break;
            case 'create.program':
                $data = $this->model->whereBetween('created_at', $range)->where('programmer_id', $uid)->where('ptype', 1)->count();
                break;
            case 'create.psd':
                $data = $this->model->whereBetween('created_at', $range)->where('programmer_id', $uid)->where('ptype', 2)->count();
                break; 
            case 'design':
                $data = $this->model->whereBetween('designed_at', $range)->where('designer_id', $uid)->where('ptype', 1)->count();
                break; 
            case 'program':
                $data = $this->model->whereBetween('produced_at', $range)->where('programmer_id', $uid)->where('ptype', 1)->count();
                break; 
            case 'psd':
                $data = $this->model->whereBetween('produced_at', $range)->where('programmer_id', $uid)->where('ptype', 2)->count();
                break; 
            case 'inputed':
                $data = $this->model->whereBetween('finished_at', $range)->where('inputer_id', $uid)->where('ptype', '<', 3)->count();
                break; 
            case 'online':
                $data = $this->model->whereBetween('finished_at', $range)->where('status', 3)->where('ptype', '<', 3)->where(function($query) use ($uid) {
                                            $query->where('designer_id', $uid)->orWhere('programmer_id', $uid);
                                    })->count();
                break; 
            default:
                # code...
                break;
        }

        return $data;
    }

   // 统计订单数量 - 按照条件
    public function getCountListsWhere($where, $groupField='designer_id')
    {
        $model = $this->model;

        foreach ($where as $field => $value) {
            if (is_array($value)) {
                if (count($value) === 3) {
                    list($field, $operator, $search) = $value;
                    $model = $model->where($field, $operator, $search);
                } elseif (count($value) === 2) {
                    list($field, $search) = $value;
                    $model = $model->where($field, '=', $search);
                }
            } else {
                $model = $model->where($field, '=', $value);
            }
        }

        return $model->select(\DB::raw("{$groupField}, count(*) as count"))->where($groupField, '>', 0)
                     ->groupBy($groupField)->lists('count', $groupField);
    }
}